<?php
/*
 * Camelot PHP Tools
 * This example illustrates how to download a List Item Attachement
 *
 * @version 2.0
 * @author Bendsoft
 * @package PHP Tools
 * @subpackage Camelot
 * @license FreeBSD License (www.bendsoft.com/licensing/)
 *
 * Note! 
 * Set up settings.php before using this file
 */
error_reporting(E_COMPILE_ERROR);
ini_set('display_errors', '1');


// Load all required files for Camelot PHP Tools
require_once '../loader.php';
?>

<?php include 'base/head.php'; ?>

<h2>Document libraries helper</h2>
<p>List all document libraries in the selected connection</p>
<form method="post">
    <div class="query">
        <label>Get an object with all document libraries</label> 
        <input type="submit" name="documentLibrary_submit" value="Submit" />
    </div>

    <div>
        <label>Render as table
            <input type="checkbox" name="renderAsTable" <?php echo ((isset($_POST['renderAsTable'])) || !$_POST ) ? 'checked="checked"' : '' ?>  />
        </label>
        <p class="comment">Meta property, render object or graphical table</p>
    </div>
</form>


<?php
if ($_POST) {

    // Stopwatch
    $time_pre = microtime(true);

    try {

        // Include hidden libraries or not
        $includeHidden = true;
        
        // Execute and collect the result
        $result = new SharePointDocumentLibraries("SharePointConnection1", $includeHidden);

        // If some form of exception occured and SILENT_EXCEPTIONS is set to TRUE
        if (!$result->commandStatus) {
            echo "<p>Silent exceptions is ON, this error message is forced to appear in this example</p>";
            $message = $result->exception->message;
            echo "<h2>Exception: $message</h2>";
            pr($result);
        } else {

            // Render result
            if (isset($_POST['renderAsTable'])) {
                
                // Convert the objects to an array (for the twig renderer)
                $twigRender = array();
                foreach ($result->CamelotDocumentLibrariesObjects as $object) {
                    array_push($twigRender, (array) $object);
                }


                // Load twig
                require_once '../twig/lib/Twig/Autoloader.php';
                Twig_Autoloader::register();
                $loader = new Twig_Loader_Filesystem('../templates');
                $twig = new Twig_Environment($loader);
                $template = $twig->loadTemplate('sp2007.html');

                echo $template->render(array(
                    'list_header' => array('Name', 'Title', 'Hidden', 'BaseType', 'ItemCount', 'DefaultViewUrl'),
                    'list_content' => $twigRender
                ));
            } else {
                // No fancy stuff, just dump the object
                pr($result);
            }
        }
    } catch (CamelotException $exc) {
        echo "<h2>Exception: $exc->message</h2>";
        pr($exc);
    }

    // ...end stopwatch
    $time_post = microtime(true);
    $exec_time = $time_post - $time_pre;
    echo "<p class=\"comment\">Query executed and data transfered in $exec_time seconds</p>";
}
?>

<?php include 'base/foot.html'; ?>